﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Latin_Vocabulary_Indicator
{
    public partial class mainproj : Form
    {
        public enum testType
        {
            review,
            test
        }
        Form _caller;
        List <ListViewItem> _wordlist;
        public testType _testType;
        Label[] topic_text=new Label[4]; Label[] topic_redline = new Label[4]; Label[] topic_corrector = new Label[4]; TextBox[] topic_wordbox = new TextBox[4];
        ListViewItem current;
        public byte[] options=new byte[4];bool doNotDisable = false;
        public byte taskLeaked=0;public short answerTime = 0;
        public ListViewItem[] detail; ListViewItem newDetailItem;
        public List<ListViewItem> _examined;
        public DateTime startTime, endTime;
        bool previousState = false;
        ListViewItem previousItem; ListViewItem nextItem;bool antidopt = false;
        public mainproj(Form caller, ListViewItem[] wordlist, testType testype, string henText)
        {
            InitializeComponent();
            InitializeArray();
            _caller = caller;
            _testType = testype;
            _wordlist = new List<ListViewItem>();
            string[] inputer = fileOperator.textInput("Latin\\options.ini").Split('\r');
            for (byte x = 0; x <= inputer.Length - 1; x++) options[x] = Convert.ToByte(inputer[x].Split(':')[1]);
            foreach (var subum in wordlist) {
                /*bool skip = false;
                foreach (var intersub in _wordlist)
                {
                    if (intersub.SubItems[1].Text==subum.SubItems[1].Text|| intersub.SubItems[3].Text == subum.SubItems[3].Text)
                    {
                        skip = true;
                        break;
                    }
                }
                if (skip) continue;*/
                if (_testType == testType.review) subum.SubItems.Add("0");
                subum.Tag =Convert.ToByte( options[0]); _wordlist.Add(subum);
            }
            title.Text = henText;
            bar.Maximum = _wordlist.Count;
            setBarValue(0);
            startTime = DateTime.Now;
            if (_testType == testType.test) detail = new ListViewItem[bar.Maximum];
            english.Text = "|nUlLa|";
            if (_testType == testType.review) _examined = new List<ListViewItem>(wordlist.Length);
            timer1.Enabled = true;
        }
        private void setBarValue(short value)
        {
            bar.Value = value;
            bar_text.Text = bar.Value + "/" + bar.Maximum;
        }
        private void InitializeArray()
        {
            topic_text[0] = latin_t1;
            topic_text[1] = latin_t2;
            topic_text[2] = latin_t3;
            topic_text[3] = latin_t4;

            topic_redline[0] = wrong_1;
            topic_redline[1] = wrong_2;
            topic_redline[2] = wrong_3;
            topic_redline[3] = wrong_4;

            topic_wordbox[0] = latin_1;
            topic_wordbox[1] = latin_2;
            topic_wordbox[2] = latin_3;
            topic_wordbox[3] = latin_4;

            topic_corrector[0] = correct_1;
            topic_corrector[1] = correct_2;
            topic_corrector[2] = correct_3;
            topic_corrector[3] = correct_4;
        }
        private void nextWord()
        {
            do {
                current = _wordlist[new Random().Next(_wordlist.Count)];
            } while (_wordlist.Count >1 && english.Text == current.SubItems[3].Text);
            english.Text = current.SubItems[3].Text;
            note.Text = current.SubItems[4].Text == "" ? "-":fileOperator.accentTransform( current.SubItems[4].Text,true);
            latin_0_Enter(null, null);
            latin_0.Focus();
            string temp = fileOperator.togglePos(current.SubItems[2].Text).Split('.')[0];
            if (temp != "m" && temp != "f" && temp != "n") { latin_0.ReadOnly = true; latin_0.Text = temp;  }
            else latin_0.ReadOnly = false;
            doNotDisable = false;
            previous.Enabled = _testType == testType.test&&!previousState&&null!=previousItem;
        }

        private byte OfferBox(string pos,byte quid=0)
        {
            latin_0.Text = pos;
            Action<byte> offer = (boxOccurs) =>
            {
                for (byte x = 0; x <= 3; x++)
                {
                    topic_wordbox[x].Clear();
                    topic_text[x].Text = "";
                    topic_wordbox[x].Visible = topic_text[x].Visible = x <= boxOccurs - 1;
                }
            };
            byte offerede = 0;
            if (pos.ToLower() == "feminine" || pos.ToLower() == "masculine" || pos.ToLower() == "neuter")
            {
                offer(2);
                topic_text[0].Text = "nominative";
                topic_text[1].Text = "genitive";
                offerede=2;
             }
            else if (pos.ToLower() == "multigender")
            {
                offer(4);
                topic_text[0].Text = "masculine:\r\nnominative";
                topic_text[1].Text = "masculine:\r\ngenitive";
                topic_text[2].Text = "feminine:\r\nnominative";
                topic_text[3].Text = "feminine:\r\ngenitive";
                offerede = 4;
            }
            else if (pos.ToLower() == "adjective")
            {
                offer(3);
                topic_text[0].Text = "masculine";
                topic_text[1].Text = "feminine";
                topic_text[2].Text = "neuter";
                offerede = 3;
            }
            else if (pos.ToLower() == "verb")
            {
                offer(quid);
                topic_text[0].Text = "active indicative present singular first";
                topic_text[1].Text = "active present infinitive";
                topic_text[2].Text = "active indicative perfect singular first";
                topic_text[3].Text = "perfect passive participle";
                offerede = quid;
            }
            else
            {
                offer(1);
                topic_text[0].Text = "[uninflected]";
                offerede = 1;
             }
            topic_text[0].Focus();

            for (byte x = 0; x <= offerede - 1; x++)
            {
                float bias =(float)( 1.0 + (offerede-1) /3.0);
                topic_text[x].Left = topic_redline[x].Left = topic_wordbox[x].Left = topic_corrector[x].Left =
                    (int)((bias * (jar.Width- latin_1.Width*offerede)) / (2.0 * bias + offerede - 1) + x * (latin_1.Width + (jar.Width - latin_1.Width * offerede) / (2.0 * bias + offerede - 1)));
            }
            next.Enabled = true;
            latin_1.Focus();
            return offerede;

        }


        private void latin_0_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (latin_0.ReadOnly)
            {
                if (e.KeyChar == (char)13 || e.KeyChar == ' ')
                {
                    e.Handled = true;
                    topic_wordbox[0].Focus(); topic_wordbox[0].SelectionStart = topic_wordbox[0].TextLength;
                }
                else return;
            }
            if (e.KeyChar == ' ') e.Handled = true;
        }

        private void latin_0_Enter(object sender, EventArgs e)
        {
            if (sender!=null&&latin_0.ReadOnly) return;
            if (latin_1.Visible)
            {
                if(!antidopt)  latin_0.Clear();
              else  antidopt = false;
                for (byte x = 0; x <= 3; x++)
                {
                    topic_wordbox[x].Clear();
                    topic_text[x].Text = "";
                    topic_wordbox[x].Visible = topic_text[x].Visible = false;
                }
                next.Enabled = false;
            }
        }

        private void latin_0_TextChanged(object sender, EventArgs e)
        {
            if (msger.Visible && !doNotDisable) msger.Visible = false;
            if (latin_0.Text.ToLower() == "m") OfferBox("Masculine");
            else if (latin_0.Text.ToLower() == "f") OfferBox("Feminine");
            else if (latin_0.Text.ToLower() == "n") OfferBox("Neuter");
            if (latin_0.ReadOnly) { 
            if (latin_0.Text.ToLower() == "v") OfferBox("Verb", (byte)current.SubItems[1].Text.Split(',').Length);
            else if (latin_0.Text.ToLower() == "adj") OfferBox("Adjective");
            else if (latin_0.Text.ToLower() == "adv") OfferBox("Adverb");
            else if (latin_0.Text.ToLower() == "pron") OfferBox("Pronoun");
            else if (latin_0.Text.ToLower() == "prep") OfferBox("Preposition");
            else if (latin_0.Text.ToLower() == "conj") OfferBox("Conjunction");
            else if (latin_0.Text.ToLower() == "int") OfferBox("Interjection");
            else if (latin_0.Text.ToLower() == "two") OfferBox("Multigender");
            else if (latin_0.Text.ToLower() == "r") OfferBox("Interrogation");
            else if (latin_0.Text.ToLower() == "\\") OfferBox("\\");}
        }

        private void correctCase(byte id)
        {
            string newstr = "";
            bool upperCase = true;
            foreach (char subchr in topic_wordbox[id].Text)
            {
                if (upperCase)
                {
                    upperCase = false;
                    newstr += fileOperator.toUpper(subchr);
                }
                else
                    newstr += fileOperator.toLower(subchr);
            }
            byte sel = (byte)topic_wordbox[id].SelectionStart;
            topic_wordbox[id].Text = newstr;
            topic_wordbox[id].SelectionStart = sel;
        }

        private void latin_1_TextChanged(object sender, EventArgs e)
        {
            if (msger.Visible && !doNotDisable) msger.Visible = false;
            correctCase(0);
        }

        private void latin_2_TextChanged(object sender, EventArgs e)
        {
            if (msger.Visible && !doNotDisable) msger.Visible = false;
            correctCase(1);
        }

        private void latin_3_TextChanged(object sender, EventArgs e)
        {
            if (msger.Visible && !doNotDisable) msger.Visible = false;
            correctCase(2);
        }

        private void latin_4_TextChanged(object sender, EventArgs e)
        {
            if (msger.Visible && !doNotDisable) msger.Visible = false;
            correctCase(3);
        }

        private void latin_1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (latin_1.ReadOnly) return;
            if (e.KeyChar == (char)13 || e.KeyChar == ' ')
            {
                e.Handled = true;
                if (topic_wordbox[1].Visible) { topic_wordbox[1].Focus(); topic_wordbox[1].SelectionStart = topic_wordbox[1].TextLength; }
                else { e.Handled = true; next_Click(null, null); }
            }
            else if (e.KeyChar == (char)8 && topic_wordbox[0].SelectionStart == 0) { e.Handled = true; latin_0.Focus(); }
            else if (e.KeyChar == '`' )
            {
                e.Handled = true;
                if (topic_wordbox[0].SelectionStart != 0){
                    topic_wordbox[0].SelectionStart--;
                    topic_wordbox[0].SelectionLength = 1;
                    topic_wordbox[0].SelectedText = fileOperator.toggleAccent(Convert.ToChar(topic_wordbox[0].SelectedText)).ToString();
                }
            }
            else if (e.KeyChar == ',')
            {
                if (topic_wordbox[0].SelectionStart == 0 || topic_wordbox[0].Text.Substring(topic_wordbox[0].SelectionStart - 1, 1) != " ") e.KeyChar = ' ';
                else e.Handled = true;
            }
        }

        private void latin_2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (latin_2.ReadOnly) return;
            if (e.KeyChar == (char)13 || e.KeyChar == ' ')
            {
                e.Handled = true;
                if (topic_wordbox[2].Visible) { e.Handled = true; topic_wordbox[2].Focus(); topic_wordbox[2].SelectionStart = topic_wordbox[2].TextLength; }
                else { e.Handled = true; next_Click(null, null); }
            }
            else if (e.KeyChar == (char)8 && topic_wordbox[1].SelectionStart == 0) { e.Handled = true; topic_wordbox[0].Focus(); topic_wordbox[0].SelectionStart = topic_wordbox[0].TextLength; }
            else if (e.KeyChar == '`')
            {
                e.Handled = true;
                if (topic_wordbox[1].SelectionStart != 0) { 
                    topic_wordbox[1].SelectionStart--;
                    topic_wordbox[1].SelectionLength = 1;
                    topic_wordbox[1].SelectedText = fileOperator.toggleAccent(Convert.ToChar(topic_wordbox[1].SelectedText)).ToString();
                }
            }
            else if (e.KeyChar == ',')
            {
                if (topic_wordbox[1].SelectionStart == 0 || topic_wordbox[1].Text.Substring(topic_wordbox[1].SelectionStart - 1, 1) != " ") e.KeyChar = ' ';
                else e.Handled = true;
            }
        }

        private void latin_3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (latin_3.ReadOnly) return;
            if (e.KeyChar == (char)13 || e.KeyChar == ' ')
            {
                e.Handled = true;
                if (topic_wordbox[3].Visible) { e.Handled = true; topic_wordbox[3].Focus(); topic_wordbox[3].SelectionStart = topic_wordbox[3].TextLength; }
                else { e.Handled = true; next_Click(null, null); }
            }
            else if (e.KeyChar == (char)8 && topic_wordbox[2].SelectionStart == 0) { e.Handled = true; topic_wordbox[1].Focus(); topic_wordbox[1].SelectionStart = topic_wordbox[1].TextLength; }
            else if (e.KeyChar == '`')
            {
                e.Handled = true;
                if (topic_wordbox[2].SelectionStart != 0) { 
                    topic_wordbox[2].SelectionStart--;
                    topic_wordbox[2].SelectionLength = 1;
                    topic_wordbox[2].SelectedText = fileOperator.toggleAccent(Convert.ToChar(topic_wordbox[2].SelectedText)).ToString();
                }
            }
            else if (e.KeyChar == ',')
            {
                if (topic_wordbox[2].SelectionStart == 0 || topic_wordbox[2].Text.Substring(topic_wordbox[2].SelectionStart - 1, 1) != " ") e.KeyChar = ' ';
                else e.Handled = true;
            }
        }

        private void latin_4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (latin_4.ReadOnly) return;
            if (e.KeyChar == (char)13 || e.KeyChar == ' ') { e.Handled = true; next_Click(null, null); }
            else if (e.KeyChar == (char)8 && topic_wordbox[3].SelectionStart == 0) { e.Handled = true; topic_wordbox[2].Focus(); topic_wordbox[2].SelectionStart = topic_wordbox[2].TextLength; }
            else if (e.KeyChar == '`')
            {
                e.Handled = true;
                if (topic_wordbox[3].SelectionStart != 0){
                    topic_wordbox[3].SelectionStart--;
                    topic_wordbox[3].SelectionLength = 1;
                    topic_wordbox[3].SelectedText = fileOperator.toggleAccent(Convert.ToChar(topic_wordbox[3].SelectedText)).ToString();
                }
            }
            else if (e.KeyChar == ',')
            {
                if (topic_wordbox[3].SelectionStart == 0 || topic_wordbox[3].Text.Substring(topic_wordbox[3].SelectionStart - 1, 1) != " ") e.KeyChar = ' ';
                else e.Handled = true;
            }
        }

        private void msg(string input)
        {
            msger.Text = input;msger.ForeColor = System.Drawing.Color.Orange;msger.Visible = true;
        }
        private void setReadOnly(bool value,bool A = true)
        {
            if(A) latin_0.ReadOnly = value;
            for (byte x = 0; x <= 3; x++) topic_wordbox[x].ReadOnly = value;
        }
        private void next_Click(object sender, System.EventArgs e)
        {
            if (latin_1.Visible == false) { msg("Please use a proper pos."); latin_0.Focus(); return; }
            for (byte x = 0; x <= 3; x++)
            {
                if (topic_wordbox[x].Visible == false) break;
                if (topic_wordbox[x].Text.Trim() == "") { msg("\"" + topic_text[x].Text + "\" shouldn't be empty."); topic_wordbox[x].Focus(); return; }
                if (topic_wordbox[x].Text.Contains("|") || topic_wordbox[x].Text.Contains("`") || topic_wordbox[x].Text.Contains(",")) { msg("char \"|\" or \"`\" or \",\" should not exist in \"" + topic_wordbox[x].Text + "\"."); topic_wordbox[x].Focus(); return; }
            }
            doNotDisable = true;
            answerTime++;
            byte testTaskState = 0;
            if (_testType == testType.review) current.SubItems[5].Text = (Convert.ToByte(current.SubItems[5].Text) + 1).ToString();
            bool accentError = false;
            bool spellingError = false;
            if (latin_0.Text != current.SubItems[2].Text)
            {
                if (_testType == testType.test) { testTaskState = 1;goto toHither;  }
                else
                {
                    msger.Text = "Part of speech mistaken";
                    latin_0.ForeColor = msger.ForeColor = System.Drawing.Color.Red;
                    msger.Visible = true;
                    msger.Focus();
                    wrong_0.Text = "";
                    wrong_0.Visible = true;
                    correct_0.Text = current.SubItems[2].Text;
                    correct_0.Visible = true;
                    antidopt = true;
                    setReadOnly(true);

                    next.Enabled = false;
                    skip.Enabled = false;
                    contrarn.Enabled = true;
                    current.Tag = Math.Max(0, Convert.ToByte(current.Tag) - options[2]);
                }
            }
            for(byte x = 0; x <= 3; x++)
            {
                Func<string, string> toggleToLower = (input) => {
                    string output = "";
                    foreach(char cha in input) { output += fileOperator.toLower(cha); }
                    return output;
                };
                if (topic_wordbox[x].Visible && toggleToLower(topic_wordbox[x].Text.Trim()) != toggleToLower((current.SubItems[1].Text.Split(',').Length == 1) ?
                    current.SubItems[1].Text.Split(',')[0].Trim() : current.SubItems[1].Text.Split(',')[x].Trim()))
                {
                    if (Deaccent(topic_wordbox[x].Text.Trim()).ToLower() == Deaccent(current.SubItems[1].Text.Split(',')[x].Trim()).ToLower())
                    {
                        accentError = true; if (_testType == testType.test) continue;
                        topic_redline[x].BackColor = topic_wordbox[x].ForeColor = System.Drawing.Color.Blue;
                    }
                    else { spellingError = true; if (_testType == testType.test) continue; topic_redline[x].BackColor = topic_wordbox[x].ForeColor = System.Drawing.Color.Red; }
                    topic_redline[x].Text = "";
                    topic_redline[x].BringToFront();
                    topic_redline[x].Visible = true;
                    topic_corrector[x].Text = current.SubItems[1].Text.Split(',')[x].Trim();
                    topic_corrector[x].Visible = true;
                }
            }
            if (latin_0.Text != current.SubItems[2].Text)
            {
                for (byte x = 0; x <= 3; x++) if (topic_wordbox[x].Visible && topic_wordbox[x].ForeColor == System.Drawing.Color.Black) topic_wordbox[x].ForeColor = System.Drawing.Color.Green;
                return;
            }
            if (spellingError)
            {
                if (_testType == testType.test) testTaskState = 2;
                else
                {
                    msger.Text = "Incorrect";
                    msger.ForeColor = System.Drawing.Color.Red;
                    msger.Visible = true;
                    msger.Focus();
                }
            }
            else if (accentError)
            {
                if (_testType == testType.test) testTaskState = 3;
                else
                {
                    msger.Text = "Accent mistaken";
                    msger.ForeColor = System.Drawing.Color.Red;
                    msger.Visible = true;
                    msger.Focus();
                }
            }
            else if (_testType == testType.test) testTaskState = 4;
            if (testTaskState==0)
            if (spellingError || accentError)
            {
                setReadOnly(true);
                next.Enabled = false;
                skip.Enabled = false;
                contrarn.Enabled = true;
                current.Tag = Math.Max(0,Convert.ToByte( current.Tag ) - options[2]);
                latin_0.ForeColor = System.Drawing.Color.Green;
                for (byte x = 0; x <= 3; x++) if (topic_wordbox[x].Visible &&  topic_wordbox[x].ForeColor == System.Drawing.Color.Black) topic_wordbox[x].ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                current.Tag = Convert.ToByte( current.Tag) + options[1];
                if (_wordlist.Count == 1)
                {
                    _examined.Add((ListViewItem)current.Clone());
                    doNotDisable = true;
                    msger.Text = "Complete!";
                    msger.ForeColor = System.Drawing.Color.Lime;
                    msger.Visible = true;
                    next.Enabled = false;
                    skip.Enabled = false;
                    cancel.Enabled = false;
                    contrarn.Enabled = true;
                    contrarn.Text = "Complete";
                    latin_0.ForeColor = System.Drawing.Color.Green;
                    for (byte x = 0; x <= 3; x++) if (topic_wordbox[x].Visible) topic_wordbox[x].ForeColor = System.Drawing.Color.Green;
                    taskLeaked = (byte)Math.Ceiling(Math.Max(0, options[0] - Convert.ToByte(_wordlist[0].Tag)) / (float)options[1]);
                    _wordlist.Remove(current);
                    setBarValue((short)(bar.Maximum - _wordlist.Count));
                    return;
                }
                else if (Convert.ToByte(current.Tag)>= options[3])
                {
                    _examined.Add((ListViewItem)current.Clone());
                    msger.Text = "Correct: Word accomplished";
                    _wordlist.Remove(current);
                    setBarValue((short)(bar.Maximum-_wordlist.Count));
                }
                else msger.Text = "Correct";
                msger.ForeColor = System.Drawing.Color.Lime;
                msger.Visible = true;
                nextWord();
            }
            //test submit area
            toHither:
            if (testTaskState != 0)
            {
               if(bar.Value - 1>=0&&!previousState) detail[bar.Value-1] = newDetailItem;
                newDetailItem = new ListViewItem();
                newDetailItem.SubItems.Add("nulla");
                newDetailItem.SubItems.Add("nulla");
                newDetailItem.SubItems.Add("nulla");
                newDetailItem.Text = bar.Value.ToString();
                //1
                newDetailItem.SubItems[1].Text = current.SubItems[3].Text;
                //2
                newDetailItem.SubItems[2].Text = "[" + fileOperator.togglePos( current.SubItems[2].Text) + "] "+ current.SubItems[1].Text;
                //3
                bool allEqual = true;
                string justTemp = "nulla";
                for (byte x = 1; x <= 3; x++)
                {
                    if (topic_wordbox[x].Visible == false) break;
                    else if (topic_wordbox[0].Text.Trim() != topic_wordbox[x].Text.Trim())
                    {
                        allEqual = false; break;
                    }
                }
                if (allEqual) justTemp = topic_wordbox[0].Text.Trim();
                else
                {
                    string trimstr = "";
                    for (byte x = 0; x <= 3; x++)
                    {
                        if (topic_wordbox[x].Visible == false) break;
                        trimstr += (x + 1 > 3 || topic_wordbox[x + 1].Visible == false) ? topic_wordbox[x].Text.Trim() : topic_wordbox[x].Text.Trim() + ", ";
                    }
                    justTemp = trimstr;
                }
                newDetailItem.SubItems[3].Text = "[" + fileOperator.togglePos( latin_0.Text ) + "] " + justTemp;
                //total
                if (testTaskState == 1) { newDetailItem.Tag = "pos mistaken"; newDetailItem.ForeColor = System.Drawing.Color.Red; }
                else if (testTaskState == 2) { newDetailItem.Tag = "incorrect"; newDetailItem.ForeColor = System.Drawing.Color.Red; }
                else if (testTaskState == 3) { newDetailItem.Tag = "accent mistaken"; newDetailItem.ForeColor = System.Drawing.Color.Orange; }
                else if (testTaskState == 4) { newDetailItem.Tag = "correct"; newDetailItem.ForeColor = System.Drawing.Color.Lime; }
                previousItem = current;
                _wordlist.Remove(current);
                setBarValue(_testType==testType.review? (short)(bar.Maximum - _wordlist.Count):(short)(bar.Value+1));
                if (previousState){ previous.Enabled = true;}
                if(previousState&&null!=nextItem) lockInto(nextItem); else check_et_nextWord();
                previousState = false;nextItem = null;
            }
        }
        private void check_et_nextWord()
        {
            if (_wordlist.Count != 0) nextWord();
            else
            {
                doNotDisable = true;
                setReadOnly(true);
                msger.Text = "Complete!";
                msger.ForeColor = System.Drawing.Color.Lime;
                msger.Visible = true;
                next.Enabled = false;
                skip.Enabled = false;
                cancel.Enabled = false;
                contrarn.Enabled = true;
                contrarn.Text = "Complete";
            }
        }
        private string Deaccent (string input)
        {
            string output = "";
            foreach(char c in input)
            {
                if (c == 'ā' || c == 'ō' || c == 'ī' || c == 'ē' || c == 'ū' || c == 'Ā' || c == 'Ō' || c == 'Ī' || c == 'Ē' || c == 'Ū')
                    output += fileOperator.toggleAccent(c);
                else output += c;
            }
            return output;
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void skip_Click(object sender, EventArgs e)
        {
            Enabled = false;
            new confirmbox("Skip?", "Skip", (returned) => {
                Enabled = true;
                Focus();
                if (returned&&_testType==testType.review)
                {
                    answerTime++;
                    msger.Text = "Skipped";
                    msger.ForeColor = System.Drawing.Color.Yellow;
                    msger.Visible = true;
                    msger.Focus();
                    current.SubItems[5].Text =( Convert.ToByte(current.SubItems[5].Text) + 1).ToString();
                    current.Tag = Math.Max(0,Convert.ToByte( current.Tag )- options[2]);
                    setReadOnly(true);
                    OfferBox(current.SubItems[2].Text);
                    latin_0.ForeColor = System.Drawing.Color.Gold;
                    for(byte x = 0; x <= 3; x++)
                    {
                        if (!topic_wordbox[x].Visible) break;
                        topic_wordbox[x].ForeColor = System.Drawing.Color.Gold;
                        topic_wordbox[x].Text = current.SubItems[1].Text.Split(',')[x].Trim();
                    }
                    next.Enabled = false;
                    skip.Enabled = false;
                    contrarn.Enabled = true;
                    note.Focus();
                    note.SelectionStart = note.TextLength;
                }
                else if (returned)
                {
                    msger.Text = "Skipped";
                    msger.ForeColor = System.Drawing.Color.Yellow;
                    msger.Visible = true;
                    msger.Focus();
                    latin_0.Text = "";
                    latin_0_Enter(null, null);
                    newDetailItem = new ListViewItem();
                    newDetailItem.Text= bar.Value.ToString();
                    newDetailItem.SubItems.Add(current.SubItems[3].Text);
                    newDetailItem.SubItems.Add("[" + fileOperator.togglePos(current.SubItems[2].Text) + "] " + current.SubItems[1].Text);
                    newDetailItem.SubItems.Add("(skipped)");
                    newDetailItem.Tag = "skipped";
                    newDetailItem.ForeColor = System.Drawing.Color.Yellow;
                    detail[bar.Value] = newDetailItem;
                    _wordlist.Remove(current);
                    setBarValue((short)(bar.Maximum - _wordlist.Count));
                    check_et_nextWord();
                }
            }).Show();
        }

        private void contrarn_Click(object sender, EventArgs e)
        {
            if (contrarn.Text == "Continue")
            {
                skip.Enabled = true;
                contrarn.Enabled = false;
                setReadOnly(false,false);
                latin_0.Visible = true;
                latin_0_Enter(null, null);
                latin_0.Text = "";
                latin_0.ForeColor = System.Drawing.Color.Black;
                wrong_0.Visible= correct_0.Visible= false;
                for (byte x = 0; x <= 3; x++) { topic_redline[x].Text = ""; topic_redline[x].Visible = topic_corrector[x].Visible = false;topic_wordbox[x].ForeColor = System.Drawing.Color.Black;topic_wordbox[x].ForeColor = System.Drawing.Color.Black; }
                nextWord();
            }
            else
            {
                endTime = DateTime.Now;
                if(_testType==testType.test) detail[bar.Value-1] = newDetailItem;
                new resultbox(this, _testType).Show();
                Hide();
            }
        }

        private void mainproj_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (fileOperator.totalQuit) return;
            if (MessageBox.Show("Do you want to quit?", "Quit", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                e.Cancel = true;
            }else
            {
                _caller.Show();
                _caller.Focus();
            }

        }

        private void previous_Click(object sender, EventArgs e)
        {
            nextItem = contrarn.Enabled?null:current;
            _wordlist.Remove(current);
            skip.Enabled = true;
            contrarn.Enabled = false;
            lockInto(previousItem);
            previous.Enabled = false;
            setBarValue((short)(bar.Value - 1));
            previousItem = null;
            previousState = true;
        }

        private void lockInto(ListViewItem locks)
        {
            setReadOnly(false,false);
            latin_0.Visible = true;
            latin_0_Enter(null, null);
            latin_0.Text = "";
            english.Text = locks.SubItems[3].Text;
            note.Text = locks.SubItems[4].Text == "" ? "-" : fileOperator.accentTransform(locks.SubItems[4].Text, true);
            current = locks;
            latin_0.Focus();
            string temp = fileOperator.togglePos(current.SubItems[2].Text).Split('.')[0];
            if (temp != "m" && temp != "f" && temp != "n") { latin_0.ReadOnly = true; latin_0.Text = temp;  }
            else latin_0.ReadOnly = false;
            doNotDisable = false;
        }

        private void timer1_Tick(object sender, EventArgs e){nextWord();timer1.Enabled = false; jar.Visible = true; }

        private void breeak_Click(object sender, EventArgs e)
        {

        }
    }
}
